<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml" lang="utf-8" xml:lang="utf-8"> 
<head> 
<meta http-equiv=Content-Type content="text/html;charset=utf-8"> 
<title>MySpace.cn</title> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>           
</head> 
<body> 
<fieldset>
	<legend><b>Request Builder</b></legend>
	<label>URL:<input type="text" value="http://kcz.myspace.cn/api/do_test.php" size="45" /></label><br /><br />
	<label>
		Method:
		<select name="">			
			<option value="post">POST</option>
			<option value="get">GET</option>
		</select>
	</label><br /><br />
	<label>Parameters:<input type="text" name="" value='{"status":"ok"}' size="45" /></label><br /><br />
	<button onclick="test()">Send</button>   
</fieldset>
<br /><br />
<fieldset>
	<legend><b>调用方法</b></legend>
	<pre>
	var loRequest={ 
		"url": "http://kcz.myspace.cn/api/do_test.php",
		"method":"get",
		"data":{"status":"hello"},
		"callback":function(str){
			alert(str);
		}
	};
	loadAjax(loRequest);	
	</pre>
</fieldset>
<script type="text/javascript"> 
function test(){
	var leURL=document.getElementsByTagName("input")[0];
	var leMethod=document.getElementsByTagName("select")[0];
	var lePara=document.getElementsByTagName("input")[1];
	var leSubmit=document.getElementsByTagName("button")[0];
	
	var loData={};
	try{
		loData=json2Obj(lePara.value);
	}catch(e){
		alert(e);
	}
	var loRequest={ 
		"url": leURL.value,
		"method":leMethod.value,
		"data":loData,
		"callback":function(str){
			alert(str);
		}
	};
	loadAjax(loRequest);	
	
}
function json2Obj(astr){
	var ljson;
	if(typeof astr == "object"){
		return astr;
	}
	astr = (astr != null) ? astr.split("\n").join("").split("\r").join("") : "";
	if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/.test(astr.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) {
		if (astr != "") {
			ljson = eval('(' + astr + ')');
			return ljson;
		}
	}
	return {};
}
function loadAjax(option,path){
	if(navigator.userAgent.indexOf("IE")!=-1){ 
		(function(){ 
			try{ 
				document.documentElement.doScroll("left"); 
				action(); 
			}catch(error) {                                                  
				setTimeout(arguments.callee, 200); 
			}                                        
		})();
	}else{ 
		action(); 
	} 
	function action(){
		var xDomain = document.domain.split(".");
		if (xDomain.length >= 2){
			document.domain=xDomain[xDomain.length-2] + "." + xDomain[xDomain.length-1];
		}
		if (!option||!option.url){ 
			return; 
		}
		var liIndex=option.url.indexOf("/",7);
		var lsFrameUrl = option.url.substring(0,liIndex) + (path||"/ajax.html");
		var lsDomin= option.url.substring(7,liIndex).split(".").join("_");
		var lsFrameName = lsDomin + "_frame"; 
		var lsFrameWrapperId = lsDomin+"_frameWrapper"; 
		var leFrameWrapper = document.getElementById(lsFrameWrapperId); 
		if (window.frames[lsFrameName] == null){ 
			leFrameWrapper = document.createElement("span"); 
			leFrameWrapper.id = lsFrameWrapperId; 
			leFrameWrapper.style.display = "none"; 
			document.body.appendChild(leFrameWrapper); 
			leFrameWrapper.innerHTML = '<iframe name='+lsFrameName+'></iframe>'; 
			listen(); 
		}else if(window.frames[lsFrameName].ajax == null){ 
			listen(); 
		}else{ 
			callback(); 
		} 
		function listen(){
			var leIframe =  leFrameWrapper.getElementsByTagName("iframe")[0]; 
			if (navigator.userAgent.indexOf("IE")!=-1) { 
				leIframe.attachEvent("onload", callback); 
			}else{ 
				leIframe.addEventListener("load", callback, false); 
			}
			leIframe.src = lsFrameUrl;                               
		}
		function callback(){
			var lsGUID="_ajax_callback"+Math.floor(Math.random()*16000).toString(16);
			window[lsGUID]=option.callback||function(){};
			option.callback=lsGUID;
			
			var lxUrl=[];
			
			if(option.data){
				for(var x in option.data){					
					lxUrl.push(x);
					lxUrl.push("=");
					lxUrl.push(option.data[x]);
					lxUrl.push("&");
				}
				lxUrl.pop();
			}
			if(option.method && option.method.toLowerCase()=="get"){
				lxUrl.unshift(option.url.indexOf("?")==-1?"?":"&");
				lxUrl.unshift(option.url);
				option.url=lxUrl.join("");
				option.data = "";
			}else{
				option.data = lxUrl.join("");
			}
			window.frames[lsFrameName].ajax(option); 
		}                               
	}                             
}                                       
</script>        
</body> 
</html>